import unittest

import openpyxl#引入处理表格的库
from openpyxl import load_workbook
import csv

#获取表格

def read_excel(filename,ws_name='login'):
    wb = load_workbook(filename)
    ws = wb[ws_name]
    return list(ws.iter_rows(min_row=2,values_only=True))#返回是列表加元组


def read_excel_list(filename,ws_name='login'):
    wb = load_workbook(filename)
    ws = wb[ws_name]
    # return list(ws.iter_rows(min_row=2,values_only=True))#返回是列表加元组
    # for row in ws.iter_rows(min_row=2,values_only=True):
    #     print(row)
    # print([list(row) for row in ws.iter_rows(min_row=2,values_only=True)])#列表生成式，返回是列表加列表
    return [list(row) for row in ws.iter_rows(min_row=2,values_only=True)]

def read_excel_dict(filename, ws_name='login'):
    wb = load_workbook(filename, read_only=True)
    ws = wb[ws_name]

    # 1. 获取表头（第一行数据作为字典的键）
    # 读取第一行所有单元格的值，转换为列表（如：["姓名", "年龄", "性别"]）
    headers = [cell.value for cell in ws[1]]  # ws[1] 表示第一行
    # print(headers)
    return [dict(zip(headers,row)) for row in ws.iter_rows(min_row=2,values_only=True)]

# # 调用函数获取“列表包字典”数据
# result = read_excel_dict(r"E:\pycharm文件\unittest\data\test_login_data.xlsx")  # 替换为你的Excel文件路径
# print(result)
# # read_excel(r"E:\pycharm文件\unittest\data\test_login_data.xlsx")

def read_text_dict(filename):
    test_cases = []
    with open(filename, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        headers = lines[0].strip().split(',')  # 第一行为标题

        for line in lines[1:]:#去掉标题
            if line.strip():  # 跳过空行
                values = line.strip().split(',')
                test_case = dict(zip(headers, values))#返回对象是列表加字典
                test_cases.append(test_case)
    return test_cases
# read_text_dict(r"E:\pycharm文件\unittest\data\test_login_data.txt")

def read_text_list(filename):
    test_cases = []
    with open(filename, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines[1:]:#去掉标题
            if line.strip():  # 跳过空行
                values = line.strip().split(',')
                test_case = list(values)#返回对象是列表加列表
                test_cases.append(test_case)
        return test_cases
# read_text_list(r"E:\pycharm文件\unittest\data\test_login_data.txt")

def read_csv_dict(filename):
    test_data = []
    with open(filename, newline='', encoding='utf-8-sig') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            test_data.append(row)
    return test_data
# read_csv_dict(r"E:\pycharm文件\unittest\data\test_login_data.csv")

def read_csv_list(filename):
        # test_data = []
        # with open(filename, 'r', encoding='utf-8-sig') as file:
        #     reader = csv.reader(file)
        #     # 逐行读取数据
        #     for row in enumerate(reader, start=1):
        #         test_data.append(row)
        # return list(test_data)[1:]
    test_data = []
    with open(filename, 'r', encoding='utf-8-sig') as file:
        reader = csv.reader(file)
        next(reader)  # 跳过标题行
        for row in reader:
            test_data.append(row)
    return test_data

    # print (reader)
# read_csv_list(r"E:\pycharm文件\unittest\data\test_login_data.csv")